package dao.Impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import common.result.SearchItem;
import common.result.TaotaoResult;
import dao.SearchDao;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Repository
public class SearchDaoImpl implements SearchDao {

    @Autowired
    private SolrServer solrServer;

    @Override
    public TaotaoResult search(SolrQuery solrQuery) throws Exception {

        QueryResponse response = solrServer.query(solrQuery);
        SolrDocumentList results = response.getResults();
        List<SearchItem> list = new ArrayList<>();
        for (SolrDocument document : results) {
            SearchItem item = new SearchItem();
            item.setId(document.get("id").toString());

            if(document.get("reportName")!=null)
            item.setReportName(document.get("reportName").toString());

            if(document.get("reportObject")!=null)
            item.setReportObject(document.get("reportObject").toString());

            if(document.get("reportTimes")!=null)
            item.setReportTimes(document.get("reportTimes").toString());

            if(document.get("SearchTimes")!=null)
            item.setSearchTimes(document.get("SearchTimes").toString());

            if(document.get("reportTypeId")!=null)
            item.setReportTypeId(document.get("reportTypeId").toString());

            //id=1455439327, item_title=荣耀 6 (H60-L01) 低配版 黑色 移动4G手机 黑色, item_sell_point=, item_price=189900, item_image=http://image.taotao.com/jd/3e4a8e04ab904601b418efda63b7b47c.jpg, _version_=1559111001726517248
//            item.setId((String) document.get("id"));
//            item.setImage((String) document.get("item_image"));
//            item.setItem_desc((String) document.get("item_desc"));
//            item.setPrice((Long) document.get("item_price"));
//            item.setSell_point((String) document.get("item_sell_point"));
//            item.setTitle((String) document.get("item_title"));
            list.add(item);
        }
        TaotaoResult result = new TaotaoResult(200,"ok",list);

        //System.out.println(list);

        return result;
    }

    public void testSolr() {
        SolrServer solrServer = new HttpSolrServer("http://192.168.25.133:8080/solr");
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", "id_test");
        document.addField("title", "title_test1");
        try {
            solrServer.add(document);
            solrServer.commit();
            System.out.println("success");
        } catch (SolrServerException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
